<?php

namespace app\model\v1\building;

use crmeb\traits\ModelTrait;
use think\Model;

class BuildingMaterialsCategory extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];



    /**
     * 主材采购页面 - 分类列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getMaterialsCategoryList($param = [])
    {
        if($param["limit"] > 0) {
            $data["data"] = self::field("*")
                ->whereNULL("delete_time")
                ->where(function ($query) use ($param) {
                    if (!empty($param["name"])) $query->where("name", 'like', '%' . $param["name"] . '%');
                    if (!empty($param["id"])) $query->where("parent_id", $param["id"]);
                })
                ->order("sort", "asc")
                ->page($param["page"], $param["limit"])
                ->select()
                ->toArray();

            $data["count"] = self::field("*")
                ->whereNULL("delete_time")
                ->where(function ($query) use ($param) {
                    if (!empty($param["name"])) $query->where("name", 'like', '%' . $param["name"] . '%');
                    if (!empty($param["id"])) $query->where("parent_id", $param["id"]);
                })
                ->count();
        }else{
            $data = self::field("*")
                ->whereNULL("delete_time")
                ->where(function ($query) use ($param) {
                    if (!empty($param["name"])) $query->where("name", 'like', '%' . $param["name"] . '%');
                    if (!empty($param["id"])) $query->where("parent_id", $param["id"]);
                })
                ->order("sort", "asc")
                ->select()->toArray();
        }

        return $data;
    }


    /**
     * 编辑建材分类
     * @param $param
     * @return mixed
     */
    public static function updateMaterialsCategory($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["name"]))          $update["name"] = $param["name"];
        if(!empty($param["parent_id"]))     $update["parent_id"] = $param["parent_id"];
        if(!empty($param["visibility"]))    $update["visibility"] = $param["visibility"];
        if(!empty($param["sort"]))          $update["sort"] = $param["sort"];
        if(!empty($param["img"]))           $update["img"] = $param["img"];
        if(!empty($param["admin_id"]))      $update["admin_id"] = $param["admin_id"];

        return self::where("id", $param["id"])->update($update);
    }


    /**
     * 建材分类管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableMaterialsCategory($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("visibility");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 0;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['visibility' => $enable]);
        return $msg;
    }




}
